package nccloud.web.hr.pub;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.hr.utils.ResHelper;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pub.lang.UFBoolean;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.framework.core.env.Locator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.convert.translate.Translator;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.processor.template.ExtBillCardConvertProcessor;
import nccloud.framework.web.processor.template.ExtGridConvertProcessor;
import nccloud.framework.web.ui.config.Area;
import nccloud.framework.web.ui.config.ITempletResource;
import nccloud.framework.web.ui.config.PageTemplet;
import nccloud.framework.web.ui.config.TempletQueryPara;
import nccloud.framework.web.ui.meta.AreaType;
import nccloud.framework.web.ui.model.GridModel;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.billcard.ExtSpecilAggBill;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCardFormulaHandler;
import nccloud.framework.web.ui.pattern.form.Form;
import nccloud.framework.web.ui.pattern.form.FormFormulaHandler;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.framework.web.ui.pattern.grid.GridFormulaHandler;
import nccloud.web.hr.templet.convert.HRFormConvertor;
import nccloud.web.hr.templet.convert.HRGridConvertor;
import nccloud.web.uapbd.commons.tree.ITreeNode;













public class HRNccTemplateUtils
{
	
  public static <T> T getVOFromForm(Class<T> clazz, Object obj) {
    String read = getObjectString(obj);
    Form form = (Form)JsonFactory.create().fromJson(read, Form.class);
    return (T)(new HRFormConvertor()).fromForm(clazz, form);
  }








  
  public static <T> T[] getVOFromGrid(Class<T> clazz, Object obj) {
    String read = getObjectString(obj);
    Grid grid = (Grid)JsonFactory.create().fromJson(read, Grid.class);
    return (T[])(new HRGridConvertor()).fromGrid(clazz, grid);
  }







  
  public static ExtSpecilAggBill[] getVOFromExtGrid(Object obj) {
    String read = getObjectString(obj);
    Grid grid = (Grid)JsonFactory.create().fromJson(read, Grid.class);
    ExtGridConvertProcessor processor = new ExtGridConvertProcessor();
    return (ExtSpecilAggBill[])processor.fromGrid(grid);
  }







  
  public static ExtSpecilAggBill getVOFromExtCard(Object obj) {
    String read = getObjectString(obj);
    ExtBillCard extCard = (ExtBillCard)JsonFactory.create().fromJson(read, ExtBillCard.class);
    ExtBillCardConvertProcessor processor = new ExtBillCardConvertProcessor();
    return (ExtSpecilAggBill)processor.fromExtBillCard(extCard);
  }







  
  private static String getObjectString(Object obj) {
    if (obj == null) {
      return null;
    }
    if (obj instanceof String) {
      return (String)obj;
    }
    if (obj instanceof IRequest) {
      return ((IRequest)obj).read();
    }
    return null;
  }






  
  public static PageTemplet getPageTemplet(String pageCode) {
    ITempletResource resource = (ITempletResource)Locator.find(ITempletResource.class);
    TempletQueryPara para = new TempletQueryPara();
    para.setPagecode(pageCode);
    return resource.query(para);
  }






  
  public static PageTemplet getPageTemplet(String appCode, String pageCode) {
    ITempletResource resource = (ITempletResource)Locator.find(ITempletResource.class);
    TempletQueryPara para = new TempletQueryPara();
    para.setPagecode(pageCode);
    para.setAppcode(appCode);
    return resource.query(para);
  }







  
  public static Form getForm(String pageCode, Object vo) {
    PageTemplet templet = getPageTemplet(pageCode);
    Form form = (new HRFormConvertor()).toForm(templet.getHeadcode(), vo);
    form.setPageid(pageCode);
    form.setTempletid(templet.getOid());
    FormFormulaHandler formFormulaHandler = new FormFormulaHandler(form);
    formFormulaHandler.handleLoadFormula();
    (new Translator()).translate(form);
    setBooleanDisplay(form);
    return form;
  }







  
  public static Form getFormWithArea(String pageCode, String areaCode, Object vo) {
    PageTemplet templet = getPageTemplet(pageCode);
    Form form = (new HRFormConvertor()).toForm(areaCode, vo);
    form.setPageid(pageCode);
    form.setTempletid(templet.getOid());
    FormFormulaHandler formFormulaHandler = new FormFormulaHandler(form);
    formFormulaHandler.handleLoadFormula();
    (new Translator()).translate(form);
    setBooleanDisplay(form);
    return form;
  }
  
  public static Form getFormWithArea(PageTemplet templet, String areaCode, Object vo) {
    Form form = (new HRFormConvertor()).toForm(areaCode, vo);
    form.setTempletid(templet.getOid());
    FormFormulaHandler formFormulaHandler = new FormFormulaHandler(form);
    formFormulaHandler.handleLoadFormula();
    (new Translator()).translate(form);
    setBooleanDisplay(form);
    return form;
  }








  
  public static Form getFormWithArea(String appCode, String pageCode, String areaCode, Object vo) {
    PageTemplet templet = getPageTemplet(appCode, pageCode);
    Form form = (new HRFormConvertor()).toForm(areaCode, vo);
    form.setPageid(pageCode);
    form.setTempletid(templet.getOid());
    FormFormulaHandler formFormulaHandler = new FormFormulaHandler(form);
    formFormulaHandler.handleLoadFormula();
    (new Translator()).translate(form);
    setBooleanDisplay(form);
    return form;
  }





  
  private static void setBooleanDisplay(Form form) {
    if (form == null) {
      return;
    }
    GridModel model = form.getModel();
    if (model == null) {
      return;
    }
    Row[] rows = model.getRows();
    for (Row row : rows) {
      Cell[] cells = row.getCells();
      for (Cell cell : cells) {
        Object value = cell.getValue();
        
        if (value instanceof UFBoolean) {
          boolean isTrue = ((UFBoolean)value).booleanValue();
          cell.setDisplay(isTrue ? 
              ResHelper.getString("uif2", "AbstractDirectPrintAction-000000") : 
              ResHelper.getString("uif2", "AbstractDirectPrintAction-000001"));
        } 
      } 
    } 
  }









  
  public static Grid getGridWithArea(PageTemplet templet, String areaCode, Object objData) {
    if (templet == null || objData == null) {
      return null;
    }
    if (StringUtils.isBlank(areaCode)) {
      for (Area area : templet.getAllAreas()) {
        if (AreaType.Table.equals(area.getAreaType())) {
          areaCode = area.getCode();
          break;
        } 
      } 
      if (StringUtils.isBlank(areaCode)) {
        return null;
      }
    } else {
      boolean isExistArea = false;
      for (Area area : templet.getAllAreas()) {
        if (AreaType.Table.equals(area.getAreaType()) && areaCode.equals(area.getCode())) {
          isExistArea = true;
          break;
        } 
      } 
      if (!isExistArea) {
        return null;
      }
    } 
    Object[] vos = format2ObjArray(objData);
    if (vos == null || vos.length == 0) {
      return null;
    }
    Grid grid = (new HRGridConvertor()).toGrid(areaCode, vos);
    grid.setPageid(templet.getPagecode());
    grid.setTempletid(templet.getOid());
    (new Translator(templet)).translate(grid);
    GridFormulaHandler gridFormulaHandler = new GridFormulaHandler(grid);
    gridFormulaHandler.handleLoadFormula();
    return grid;
  }







  
  public static Grid getGrid(String pageCode, Object objData) {
    PageTemplet templet = getPageTemplet(pageCode);
    
    return getGridWithArea(templet, null, objData);
  }








  
  public static Grid getGridWithArea(String pageCode, String areaCode, Object objData) {
    PageTemplet templet = getPageTemplet(pageCode);
    
    return getGridWithArea(templet, areaCode, objData);
  }








  
  public static Grid getGridWithArea(String appCode, String pageCode, String areaCode, Object objData) {
    PageTemplet templet = getPageTemplet(appCode, pageCode);
    
    return getGridWithArea(templet, areaCode, objData);
  }








  
  public static ExtBillCard getExtBillCard(String pageCode, ExtSpecilAggBill vo) { return getExtBillCard(null, pageCode, vo, null); }










  
  public static ExtBillCard getExtBillCard(String pageCode, ExtSpecilAggBill vo, List<String> extraArea) { return getExtBillCard(null, pageCode, vo, extraArea); }

  
  public static ExtBillCard getExtBillCard(String appCode, String pageCode, ExtSpecilAggBill vo, List<String> extraArea) {
    ExtBillCardConvertProcessor processor = null;
    if (StringUtils.isBlank(appCode)) {
      processor = (extraArea == null) ? new ExtBillCardConvertProcessor() : new ExtBillCardConvertProcessor(extraArea);
    } else {
      PageTemplet templet = getPageTemplet(appCode, pageCode);
      processor = (extraArea == null) ? new ExtBillCardConvertProcessor(templet) : new ExtBillCardConvertProcessor(extraArea, templet);
    } 
    ExtBillCard billCard = processor.convert(pageCode, vo);
    if("60130705p".equals(billCard.getPageid()) || "60130709p".equals(billCard.getPageid())) {
    	  Grid[] allgr2 = billCard.getAllBodys();
    	    for(Grid grid : allgr2) {
    	    	GridModel gridmodel = grid.getModel();
    	    	Row[] rows = gridmodel.getRows();
    	    	for(Row row : rows) {
    	    		Map<String, Cell> map = row.getValues();
    	    		Cell cell = map.get("pk_jobgrade");
    	    		if(null != cell) {
    	    			String value = (String)cell.getValue();
        	    		String quesql ="select name from om_joblevel where dr=0 and pk_joblevel = '"+value+"'";
        	    		String resv="";
        	    		try {
        	    			String hvos = (String) new BaseDAO().executeQuery(quesql, new ColumnProcessor());
        	    			resv = hvos;
//        	    			if(!hvos.isEmpty()) {
//        	    				resv = hvos.get(0);
//        	    			}
        	    		}catch(Exception e){
        	    			e.printStackTrace();
        	    		}
        	    		cell.setDisplay(resv);
        	    		map.put("pk_jobgrade",cell);
    	    		}
    	    	}
    	    }
    }
  
    (new Translator()).translate(billCard);
    ExtBillCardFormulaHandler formulaHandler = new ExtBillCardFormulaHandler(billCard);
    formulaHandler.handleLoadFormula();
    formulaHandler.handleBodyLoadFormula();
    if("60130705p".equals(billCard.getPageid()) || "60130709p".equals(billCard.getPageid())) {
    	 Grid[] allgr = billCard.getAllBodys();
    	    for(Grid grid : allgr) {
    	    	GridModel gridmodel = grid.getModel();
    	    	Row[] rows = gridmodel.getRows();
    	    	for(Row row : rows) {
    	    		Map<String, Cell> map = row.getValues();
    	    		Cell cell = map.get("pk_jobgrade");
    	    		if(null != cell) {
    	    			String value = (String)cell.getValue();
        	    		String quesql ="select name from om_joblevel where dr=0 and pk_joblevel = '"+value+"'";
        	    		String resv="";
        	    		try {
        	    			String hvos = (String) new BaseDAO().executeQuery(quesql, new ColumnProcessor());
        	    			resv = hvos;
//        	    			if(!hvos.isEmpty()) {
//        	    				resv = hvos.get(0);
//        	    			}
        	    		}catch(Exception e){
        	    			e.printStackTrace();
        	    		}
        				
        	    		cell.setDisplay(resv);
        	    		map.put("pk_jobgrade",cell);
    	    		}
    	    	}
    	    }
    }
    return billCard;
  }








  
  public static ExtBillCard getExtBillCard(PageTemplet templet, ExtSpecilAggBill vo, List<String> extraArea) {
    ExtBillCardConvertProcessor processor = (extraArea == null) ? new ExtBillCardConvertProcessor(templet) : new ExtBillCardConvertProcessor(extraArea, templet);
    ExtBillCard billCard = processor.convert(templet.getPagecode(), vo);
    ExtBillCardFormulaHandler formulaHandler = new ExtBillCardFormulaHandler(billCard);
    formulaHandler.handleLoadFormula();
    formulaHandler.handleBodyLoadFormula();
    (new Translator(templet)).translate(billCard);
    return billCard;
  }





  
  public static List<ITreeNode> getBDTree(Object[] vos) {
    if (ArrayUtils.isEmpty(vos)) {
      return null;
    }
    return (new BDTreeWapper(vos)).getTree();
  }






  
  public static List<ITreeNode> getBDTree(Object[] vos, Comparator<ITreeNode> comparator) {
    if (ArrayUtils.isEmpty(vos)) {
      return null;
    }
    return (new BDTreeWapper(vos, comparator)).getWaClassItemTree();
  }







  
  private static Object[] format2ObjArray(Object objData) {
    if (objData == null) {
      return null;
    }
    if (objData.getClass().isArray()) {
      return (Object[])objData;
    }
    return new Object[] { objData };
  }
}